<?php
defined('SYSPATH') or die ('No direct access allowed.');
/**
 * Created by PhpStorm.
 * User: nirui
 * Date: 17-6-19
 * Time: 下午5:39
 */
class Controller_Newreport_Purchaseorderdetails extends Controller_Admin{

    /**
     * Created by PhpStorm.
     * User: nirui
     * Date: 17-6-18
     * Time: 下午11:46
     */

    /**
     *在途采购单信息查询
     */
    public function before(){
        try{
            ACL::Required('newreport purchaseorderdetails');
        }catch(HTTP_Exception_403 $e){
            Message::error('sorry，你没有权限进这个目录');
            $this->request->redirect(URL::base(TRUE));
        }
        parent::before();
    }

    /**
     * 在途采购单查询页面默认模板
     */
    public function action_index(){
        set_time_limit(0);
        $this->title    = __('采购单明细信息查询');
        $view           = View::factory('newreport/purchaseorderdetails');
        $view->set('enterprise_dominants',disaster::getEnterprise_dominantsByIds());
        $view->set('export_tax_rebates',disaster::getExport_tax_rebatesByIds());
        $view->set('warehouses',disaster::getWarehousesByIds());
        $view->set('purchaseorderstatuses',disaster::getAllPurchaserorderstatuses());
        $view->set('stores',disaster::getAllStores());
        $view->set('payways',disaster::getAllPayways());
        $view->set('purchaser',disaster::getAllPurchasers());
        $view->set('suppliers',disaster::getAllSuppliers());
        $view->set('transfer_hopper',disaster::getTransfer_hoppersByIds());
        $view->set('transfer_types',disaster::getTransfer_typesByIds());

        if(isset($_GET['checksign'])&&$_GET['checksign']==1){
            $count=Purchaseorderdetails::countData($_GET);
            $title=Purchaseorderdetails::$title;
            if(isset($_GET['download'])){
                $filename='disk/'.Purchaseorderdetails::getFilename($_GET).date('YmdHi').'.csv';
                if($count>Purchaseorderdetails::$maxDownloadNumber){
                    exit('1qw');
                }
                if($count>Purchaseorderdetails::$downloadLimitNumber){
                    $i=ceil($count/Purchaseorderdetails::$downloadLimitNumber);
                    $startCount=0;
                    for($c=1;$c<=$i;$c++){
                        $limitsql=" LIMIT ".($startCount).",".Purchaseorderdetails::$downloadLimitNumber;
                        $infos=Purchaseorderdetails::getData($_GET,$limitsql);
                        $infos=Purchaseorderdetails::transform($infos);
                        Purchaseorderdetails::download(array_keys($title=Purchaseorderdetails::$title),$filename
                            ,$title=Purchaseorderdetails::$title,$infos,1);
                        $startCount=$c*Purchaseorderdetails::$downloadLimitNumber;
                    }
                }else{
                    $infos=Purchaseorderdetails::getData($_GET);
                    $infos=Purchaseorderdetails::transform($infos);
                    Purchaseorderdetails::download(array_keys($title=Purchaseorderdetails::$title),$filename
                        ,$title=Purchaseorderdetails::$title,$infos);
                }
                exit($filename);
            }else{
                $view->set('title',$title);
                if($count>0){

                    $pagination = Pagination::factory(array(
                        'items_per_page' => 30,
                        'total_items' => $count,
                        'uri' => $this->request->uri(),
                    ));
                    $current_page=isset($_GET['page'])?$_GET['page']:1;
                    $start_page=($current_page==0)?0:($current_page-1)*30;
                    $view->set('pagination',$pagination);

                    $limitsql=" LIMIT ".($start_page).",30";
                    $infos=Purchaseorderdetails::getData($_GET,$limitsql);

                    $skus=array();
                    foreach($infos as &$info){
                        array_push($skus,$info['sku']);
                    }
                    $infos=Purchaseorderdetails::transform($infos);
                    $view->set('infos',$infos);
                }
            }
            if(!empty($infos)){
                if(!empty($skus)){
                }
            }
        }
        $this->response->body($view);

    }
}